import React, { useEffect } from "react";
import { useHistory, useLocation } from "react-router";
import { AppIndex } from "@anyshare/components";
import { useAuth, useLanguage } from "@anyshare/common/lib/hooks";

export default function IndexPage() {
    const history = useHistory();
    const language = useLanguage();
    const auth = useAuth();
    const { search } = useLocation();
    const redirectLogin = new URLSearchParams(search).get("redirect");
    const tokenid = new URLSearchParams(search).get("tokenid");

    useEffect(() => {
        if (redirectLogin && tokenid) {
            history.replace(`${redirectLogin}&tokenid=${tokenid}`);
            return;
        }
        if (auth.oauth2Token?.access_token && auth.oauth2Token?.id_token) {
            history.replace(`/${language.language}/dir`);
        } else if (auth.oauth2Token?.access_token || auth.oauth2Token?.id_token) {
            auth.clearLoginToken();
        }
    }, [auth, history, language]);

    return <AppIndex />;
}
